var gridColumns=[new Ext.grid.RowNumberer(), {
	header: "证书编号",
	align : 'center',
    hidden: true,
    resizable: true,
    dataIndex: "reportNo"
}, {
    header: "商品编号",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 100,
    id : "batch",
    dataIndex: "batch"
}, {
    header: "证书类型",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 80,
    id : "lab",
    dataIndex: "lab",
    renderer:function(value, metaData, record, rowIndex, colIndex, store){
    	var weightValue = Ext.util.Format.number(record.data.weight,'0.00');
		return '<a href="http://www2.gia.edu/reportcheck/index.cfm?fuseaction=home.showReportVerification&reportno='+record.data.reportNo+'&weight='+weightValue+'" target="_blank">'+value+'</a>';
	}
    
}, {
    header: "钻 重",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "weight",
    dataIndex: "weight",
    renderer : Ext.util.Format.numberRenderer('0.00')
}, {
    header: "形 状",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "shape",
    dataIndex: "shape"
}, {
    header: "净 度",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "clarity",
    dataIndex: "clarity"
}, {
    header: "颜 色",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "color",
    dataIndex: "color"
}, {
    header: "抛 光",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "polish",
    dataIndex: "polish"
}, {
    header: "荧光",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 60,
    id : "fluor",
    dataIndex: "fluor"
}, {
    header: "对 称  性",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 80,
    id : "symmetry",
    dataIndex: "symmetry"
}, {
    header: "切 工",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 80,
    id : "finalCut",
    dataIndex: "finalCut"
}, {
    header: "进货价(供应商价格)",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 120,
    id : "supPrice",
    dataIndex: "supPriceFomat"
}, {
    header: "出货价(经销商价格)",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 120,
    id : "midPrice",
    dataIndex: "midPriceFomat"
},{
    header: "供应商",
    align : 'center',
    sortable: true,
    resizable: true,
    width : 120,
    id : "supplierName",
    dataIndex: "supplierName"
}]
function getFieldsFromColumns(colums){
	var fs=new Array();
	Ext.each(colums,function(col){
		fs.push(col.dataIndex);
	})
	return fs;
}
var gridModel=new Ext.grid.ColumnModel({
  defaultSortable: true,
  defaultWidth: 85,
  columns:gridColumns
});
var jsonReader=new Ext.data.JsonReader({
	
});
var gridStore=new Ext.data.JsonStore({
	proxy: new Ext.data.HttpProxy({
        url: 'queryDiamond.action',
        failure: function(){
            Ext.MessageBox.alert("错误", "数据源获取失败!");
        }
    }),
    totalProperty: 'obj.totalSize',
    successProperty: 'success',
    root: 'obj.returnBolist',
    messageProperty: 'msg',
    fields : getFieldsFromColumns(gridColumns),
	listeners:{
		load:function(store){
		}
	}   
});
var diamondGrid = new Ext.grid.GridPanel({
	store: gridStore,
	cm: gridModel,
	frame: true,
		bbar : new Ext.PagingToolbar({
				pageSize : 100,
				store : gridStore,
				displayInfo:true,
				displyMsg : '显示第{0}条到第{1}条记录，一共{2}条',
				emptyMsg : "没有记录"
			})
});

var detailPanel=new Ext.FormPanel({
	layout:'column',
	frame:true,
	regetion:'north',
	defaults:{
		anchor: '80%',
		columnWidth:0.16,
		layout:'form',
		labelWidth:40,
		defaults:{
			anchor: '90%',
			xtype:'multiselect',
			editable : false,
			allowBlank:true,
			mode:"local",
			triggerAction : 'all',
			displayField : 'text',
			valueField : 'value'
		}
	},
	items:[{
		columnWidth:0.1,
		items:[{
			fieldLabel:'重量',
			name : 'weightFrom',
			id : 'weightFrom_id',
			xtype:'numberfield'
		},{
			fieldLabel:'价格',
			name : 'priceFrom',
			id : 'priceFrom_id',
			xtype:'numberfield'
		}]
	},{
		columnWidth:0.07,
		labelWidth:12,
		items:[{
			labelSeparator:'-',
			fieldLabel:'-',
			name : 'weightTo',
			id : 'weightTo_id',
			xtype:'numberfield'
		},{
			labelSeparator:'-',
			fieldLabel:'-',
			name : 'priceTo',
			id : 'priceTo_id',
			xtype:'numberfield'
		}]
	},{
		columnWidth:0.18,
		labelWidth:49,
		items:[{
			fieldLabel:'颜&nbsp;&nbsp;&nbsp;色',
			name : 'colour',
			id : 'colour_id',
			store:colorStore
		},{
			fieldLabel:'对称性',
			id:'symmetry_id',
			name:'symmetry',
			store:synStore
		}]
	},{
		items:[{
			fieldLabel:'净度',
			name:'purity',
			id:'purity_id',
			store:netStore
		},{
			fieldLabel:'荧光',
			name:'fluorescence',
			id:'fluorescence_id',
			store:lightStore
		}]
	},{	
		columnWidth:0.18,
		labelWidth:60,
		items:[{
			fieldLabel:'切&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;工',
			name:'cutter',
			id:'cutter_id',
			store:catTypeStore
		},{
			fieldLabel:'证书类型',
			name:'cerificate',
			id:'cerificate_id',
			store:creditStore
		}]
	},{
		labelWidth:45,
		items:[{
			fieldLabel:'抛&nbsp;&nbsp;&nbsp;光',
			name : 'buffing',
			id:'buffing_id',
			store:polishStore
		},{
			xtype:'button',
			text:'查询',
			id:'searchBt',
			anchor: '50%',
			handler: function(){
				queryDiamond(panel_gatherData(detailPanel));
			}
		}]
	}]
});

var config={
		 store: null,//因为后续可能需要处理分页，因此此处一般不直接传递GridPanel的数据源
		 title: 'gggggggggggggggggggg'//需要显示标题
		};

var topbar = [{
    icon: 'back/extJs/images/image_add.png',
    xtype: 'button',
    text: "导入钻石数据",
    handler: uploadDiamondWindow
},'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
	{
	icon : 'back/extJs/images/system/download.png',
	xtype: 'button',
    text: "导出钻石数据",
	handler : exportExcel
}];


function queryDiamond(data) {
	var store=diamondGrid.getStore();
	Ext.apply(store.baseParams, {
		json:Ext.encode(data)
	})
	store.load({});
}

function exportExcel() {
	//拼接导出文件名
	var weightFrom = Ext.get('weightFrom_id').getValue().toString();
	var weightTo = Ext.get('weightTo_id').getValue().toString();
	var priceFrom = Ext.get('priceFrom_id').getValue().toString();
	var priceTo = Ext.get('priceTo_id').getValue().toString();
	var dlFileName = "DMX";
	'DMX-diaData' + (new Date().getMonth()+1) + "_" + new Date().getDate() + "_" + new Date().getMilliseconds() + '.xls'
	if (!Ext.isEmpty(weightFrom) && !Ext.isEmpty(weightTo) && !Ext.isEmpty(priceFrom) && !Ext.isEmpty(priceTo)) {
		dlFileName = dlFileName + "("+weightFrom+"-"+weightTo+"&"+priceFrom+"-"+priceTo+")diaData"
		+(new Date().getMonth()+1).toString()+"_"+ new Date().getDate().toString()+"_"+new Date().getMilliseconds().toString() + '.xls';
	} else if ((!Ext.isEmpty(weightFrom) && !Ext.isEmpty(weightTo))&&(Ext.isEmpty(priceFrom) || Ext.isEmpty(priceTo))) {
		dlFileName = dlFileName + "("+weightFrom+"-"+weightTo+")diaData"
		+(new Date().getMonth()+1).toString()+"_"+ new Date().getDate().toString()+"_"+new Date().getMilliseconds().toString() + '.xls';
	} else if ((Ext.isEmpty(weightFrom) || Ext.isEmpty(weightTo))&&(!Ext.isEmpty(priceFrom) && !Ext.isEmpty(priceTo))) {
		dlFileName = dlFileName + "("+priceFrom+"-"+priceTo+")diaData"
		+(new Date().getMonth()+1).toString()+"_"+ new Date().getDate().toString()+"_"+new Date().getMilliseconds().toString() + '.xls';
	} else {
		dlFileName = dlFileName + "diaData"
		+(new Date().getMonth()+1).toString()+"_"+ new Date().getDate().toString()+"_"+new Date().getMilliseconds().toString() + '.xls';
	}
	
	//导出数据
	var tmpStore = diamondGrid.getStore();
	var num = tmpStore.getTotalCount();
	if (num == 0) {
		Ext.Msg.show({ 
        	title : "<span style='color:red;font-weight:bold;'>错误操作提示</span>", 
            msg : '请先查询您所需要导出的数据！', 
            buttons : Ext.Msg.OK, 
            minWidth:200,//设置宽度 
            icon : Ext.Msg.ERROR 
        });
		return ;
	} else if (num > 6000) {
		Ext.Msg.show({ 
        	title : "<span style='color:red;font-weight:bold;'>数据过大提示</span>", 
            msg : '数据过大,每次最多导出数据6000条<BR>请您缩小查询范围！！', 
            buttons : Ext.Msg.OK, 
            minWidth:220,//设置宽度 
            icon : Ext.Msg.WARNING 
        });
		return ;
	}
	
	diamondGrid.getEl().mask("正在获取数据，请稍后......");
	var tmpExportContent = '';
	// 以下处理分页grid数据导出的问题，从服务器中获取所有数据，需要考虑性能
	var tmpAllStore = new Ext.data.GroupingStore( {// 重新定义一个数据源
		proxy : tmpStore.proxy,
		reader : tmpStore.reader
	});
	var data = panel_gatherData(detailPanel);
	data["downloadAllData"] = "Y";
	Ext.apply(tmpAllStore.baseParams, {
		json : Ext.encode(data)
	});
	var diamondGridAll = new Ext.grid.GridPanel({
		store: tmpAllStore,
		cm: gridModel,
		frame: true
	});
	tmpAllStore.load();// 获取所有数据
	tmpAllStore.on('load', function(store) {
    	config.store = store;
     	tmpExportContent = diamondGridAll.getExcelXml(false, config);//此方法用到了一中的扩展
		if (Ext.isIE || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3 || true) {// 在这几种浏览器中才需要，IE8测试不能直接下载了
			if (!Ext.fly('frmDummy')) {
				var frm = document.createElement('form');
				frm.id = 'frmDummy';
				frm.name = id;
				frm.className = 'x-hidden';
				document.body.appendChild(frm);
			}
			Ext.Ajax.request( {
				url : 'back/jsp/exportDiamond.jsp',// 将生成的xml发送到服务器端
				method : 'POST',
				form : Ext.fly('frmDummy'),
				callback : function(o, s, r) {
					// alert(r.responseText);
				},
				isUpload : true,
				params : {
					ExportContent : tmpExportContent,
					ExportFile : dlFileName
				}
			});
		} else {
			document.location = 'data:application/vnd.ms-excel;base64,' + Base64.encode(tmpExportContent);
		}
		diamondGrid.getEl().unmask();
	});
}



Ext.onReady(function() {
	new Ext.Viewport({
		xtype: 'panel',
		layout: 'fit',
		items: [{
			xtype: 'panel',
			tbar: topbar,
			layout: 'border',
			items : [{
				region : 'north',
				split : true,
				border : false,
				height : 65,
				collapseMode : 'mini',
				layout : 'fit',
				items : [detailPanel]
			}, {
				region : 'center',
				split : true,
				border : false,
				layout : 'fit',
				items : [diamondGrid]
			}]
		}]
	});
});
  